package com.ymatou.iapi.live.testcase;

import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.mongodb.BasicDBList;
import com.ymatou.iapi.live.parameter.GetLiveInfoBean;
import com.ymatou.iapi.live.service.GetLiveInfoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.LiveCallService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.mongo.LiveModelWapper;
import com.ymttest.database.mongo.ProductBrandInfoWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;

public class Ts_GetLiveInfo {
	
	private static GetLiveInfoBean getLiveInfoBean;
	private static GetLiveInfoCall getLiveInfoCall;
	
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	private static String sellerName = EnvSetup.getData("productsellername");
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取直播详情");
	}

	@Before
	public void caseUp() throws Exception {
		getLiveInfoBean = new GetLiveInfoBean();
		getLiveInfoCall = new GetLiveInfoCall();

	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getLiveInfoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetLiveInfo_001() {
		 Logger.start(true,"无在售商品，正在进行的，直播接口数据与mongdb验证");
		 try {
			 int liveId = LiveCallService.createLiveInProgress(sellerName, sellerId).getJSONObject("Data").getInt("ActivityId");
			 //int liveId = 155349;
			 getLiveInfoBean.setLiveId(liveId);
			 getLiveInfoBean.setUserId(4085);
			 
			 getLiveInfoCall.setData(getLiveInfoBean);
			 getLiveInfoCall.callService();
			 
			 this.normalVerifyData(getLiveInfoCall, liveId);
			 
		 } catch (Exception e) {
			 Logger.fail(e);
		 }
	 }
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetLiveInfo_002() {
		 Logger.start(true,"有在售商品，正在进行的，直播接口品牌品类数据与mongdb验证");
		 try {
			 int liveId = LiveCallService.createLiveInProgress(sellerName, sellerId).getJSONObject("Data").getInt("ActivityId");
			 String productId = SellerProductCallService.addNewProductOnSale(sellerId, 
					 sellerName, null).getJSONObject("Data").getString("ProductID");
			 SellerProductCallService.addProductInLive(sellerId, liveId, productId);
			 
			 getLiveInfoBean.setLiveId(liveId);
			 getLiveInfoBean.setUserId(4085);
			 
			 getLiveInfoCall.setData(getLiveInfoBean);
			 LiveModelWapper wapper = new LiveModelWapper();
			 String brandEnName = ((BasicDBList) wapper.getActivityId(liveId).get("Brands")).get(0).toString();
			 
			 getLiveInfoCall.callService();
			 
			 ProductBrandInfoWapper productBrandInfoWapper = new ProductBrandInfoWapper();
			 
			 Logger.verifyEquals(productBrandInfoWapper.getProductBrandInfoByBrandEnName(brandEnName).get(0).get("BrandId").toString(), 
					 new JSONObject(new JSONObject(getLiveInfoCall.getBrandOrCategoryArray(2).get(0).toString()).getJSONArray("list").
							 get(0).toString()).getString("id"), "验证直播品牌id是否与mongo一致");
			 
			 Logger.verifyEquals(((BasicDBList) wapper.getActivityId(liveId).get("Brands")).get(0), 
					 new JSONObject(new JSONObject(getLiveInfoCall.getBrandOrCategoryArray(2).get(0).toString()).getJSONArray("list").
							 get(0).toString()).getString("name"), "验证直播品牌名称是否与mongo一致");
			 
		 } catch (Exception e) {
			 Logger.fail(e);
		 }
	 }
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetLiveInfo_003() {
		 Logger.start(true,"无在售商品，已经结束的，直播接口数据与mongdb验证");
		 try {
			 int liveId = LiveCallService.createLiveInProgress(sellerName, sellerId).getJSONObject("Data").getInt("ActivityId");
			 LiveCallService.closeLive(liveId, sellerId);
			 
			 getLiveInfoBean.setLiveId(liveId);
			 getLiveInfoBean.setUserId(4085);
			 
			 getLiveInfoCall.setData(getLiveInfoBean);
			 getLiveInfoCall.callService();
			 
			this.normalVerifyData(getLiveInfoCall, liveId);
		 } catch (Exception e) {
			 Logger.fail(e);
		 }
	}
	
	public int getActivityState(Date endTime) {
		int state = 2;
		
		if (YMTDateUtil.diffMs(endTime, new Date()) >= 0)
			 state = 1;
		 else
			 state = 0;
		
		return state;
	}
	
	public void normalVerifyData(GetLiveInfoCall getLiveInfoCall, int liveId) throws JSONException, Exception {
		LiveModelWapper wapper = new LiveModelWapper();
		
		Logger.verifyEquals(wapper.getActivityId(liveId).get("ActivityId"), getLiveInfoCall.getResult().getInt("id"), 
				"验证直播id是否与mongodb一致");
		Logger.verifyEquals(wapper.getActivityId(liveId).get("ActivityContent"), getLiveInfoCall.getResult().getString("description"), 
				"验证直播描述是否与mongodb一致");
		Logger.verifyEquals(YMTDateUtil.CST2TimeStamp(((Date) wapper.getActivityId(liveId).get("EndTime")).toString()), 
				getLiveInfoCall.getResult().getLong("expireTime"), "验证直播过期时间是否与mongodb一致");
		Logger.verifyEquals(this.getActivityState((Date) wapper.getActivityId(liveId).get("EndTime")), 
				getLiveInfoCall.getResult().getInt("state"), "验证直播状态是否与mongo一致");
		Logger.verifyEquals(( wapper.getActivityId(liveId).get("SellerId")), 
				getLiveInfoCall.getResult().getJSONObject("sellerInfo").getInt("id"), "验证买手信息中直播编号是否与mongo一致");
		Logger.verifyEquals(( wapper.getActivityId(liveId).get("ActivityName")), 
				getLiveInfoCall.getResult().getJSONObject("sellerInfo").getJSONObject("live").getString("name"),"验证买手信息中直播名称是否与mongo一致");
		Logger.verifyEquals(( wapper.getActivityId(liveId).get("ShopAddress")), 
				getLiveInfoCall.getResult().getJSONObject("sellerInfo").getJSONObject("live").getString("shopAddress"),"验证买手信息中商家店铺地址是否与mongo一致");
		Logger.verifyEquals(( wapper.getActivityId(liveId).get("Flag")), 
				getLiveInfoCall.getResult().getJSONObject("sellerInfo").getString("flag"),"验证买手信息中商家国旗是否与mongo一致");
		Logger.verifyEquals(( wapper.getActivityId(liveId).get("Country")), 
				getLiveInfoCall.getResult().getJSONObject("sellerInfo").getString("country"),"验证买手信息中商家国是否与mongo一致");
		Logger.verifyEquals(( wapper.getActivityId(liveId).get("VideoCover")), 
				getLiveInfoCall.getResult().getJSONObject("videoInfo").getString("thumbUrl"),"验证买手信息中视频封面是否与mongo一致");
		Logger.verifyEquals(( wapper.getActivityId(liveId).get("Title")), 
				getLiveInfoCall.getResult().getJSONObject("videoInfo").getString("title"),"验证买手信息中视频title是否与mongo一致");
		Logger.verifyEquals(( wapper.getActivityId(liveId).get("VideoUrl")), 
				getLiveInfoCall.getResult().getJSONObject("videoInfo").getString("videoUrl"),"验证买手信息中视频地址是否与mongo一致");
		Logger.verifyEquals(( wapper.getActivityId(liveId).get("ShopAddress")), 
				getLiveInfoCall.getResult().getJSONObject("sellerInfo").getJSONObject("live").getString("shopAddress"),"验证买手信息中商家地址是否与mongo一致");
		Logger.verifyEquals(( wapper.getActivityId(liveId).get("ActivityContent")), 
				getLiveInfoCall.getResult().getJSONObject("sellerInfo").getJSONObject("live").getString("description"),"验证买手信息中直播简介是否与mongo一致");
	}
}
